package fms.mqAccessUtil;

import fms.controller.*;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.util.Observable;
import java.util.Observer;

// Observable对象之间可以相互通知变化，使对方做出相应的更新
public class ActivemqReceiver{
//public class ActivemqReceiver extends Observable {

    private Logger logger = Logger.getLogger(ActivemqReceiver.class);

    // 负责消息的传递、搬运
    private JmsTemplate jmsTemplate;
    private WindController windController;
    private WindAcarsController windAcarsController;
    private PosController posController;
    private AutoPosController autoPosController;
    private FlpController flpController;

    private MasController masController;

    public static final int TYPE_MAS=0;
    public static final int TYPE_FPN=1;
    public static final int TYPE_PWI=2;
    public static final int TYPE_UNDEFINED=-1;

// ActiveMQDestination 用于topic模式
    // 作为接受者、消费者的ActiveMQQueue标识着“消费地址、商店”；
    private ActiveMQQueue destination;

    private boolean lastConnectionStatus;

    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public WindController getWindController() {
        return windController;
    }

    public WindAcarsController getWindAcarsController() {
        return windAcarsController;
    }

    public void setWindAcarsController(WindAcarsController windAcarsController) {
        this.windAcarsController = windAcarsController;
    }

    public FlpController getFlpController() {
        return flpController;
    }

    public void setFlpController(FlpController flpController) {
        this.flpController = flpController;
    }

    public void setWindController(WindController windController) {
        this.windController = windController;
    }

    public PosController getPosController() {
        return posController;
    }

    public void setPosController(PosController posController) {
        this.posController = posController;
    }

    public MasController getMasController() {
        return masController;
    }

    public void setMasController(MasController masController) {
        this.masController = masController;
    }


    public ActiveMQDestination getDestination() {
        return destination;
    }

//    public void setDestination(ActiveMQTopic destination) {
//        this.destination = destination;
//    }

    public void setDestination(ActiveMQQueue destination) {
        this.destination = destination;
    }

//    public void addObserver(Observer observer) {
//        super.addObserver(observer);
////        logger.info("被观察对象添加观察者");
//    }

    public AutoPosController getAutoPosController() {
        return autoPosController;
    }

    public void setAutoPosController(AutoPosController autoPosController) {
        this.autoPosController = autoPosController;
    }

    public void receive() throws JMSException {
        // 添加观察者对象
//        this.addObserver(windController);
//        this.addObserver(windAcarsController);
//        this.addObserver(posController);
//        this.addObserver(autoPosController);
//        this.addObserver(flpController);
//        this.addObserver(masController);
        lastConnectionStatus=true;
        while (true) {
            try {
                TextMessage txtmsg = (TextMessage) jmsTemplate
                        .receive(jmsTemplate.getDefaultDestinationName());
                if (null != txtmsg.getText()) {
                    // 线程安全的标记（我已经发生变化，可以通知观察者）
//                    this.setChanged();
                    logger.debug("[MQ消息获取] 收到消息内容为: \r\n" + txtmsg.getText());
                    // 通知所有观察者“我已发生变化，请做出响应”
//                    this.notifyObservers(txtmsg.getText());
                    String rawMsg=txtmsg.getText();
                    int type=getMessageType(rawMsg);
                    switch (type){

                        case TYPE_MAS:masController.receiveMsg(rawMsg);break;
                        case TYPE_FPN:flpController.receiveMsg(rawMsg);break;
                        case TYPE_PWI:windController.receiveMsg(rawMsg);break;
                        case TYPE_UNDEFINED:handleInvalidMsg(rawMsg);break;
                        default:break;
                    }
                    // 线程安全的标记（我的未发生变化or变化已结束）
//                    this.clearChanged();
                }
                else {
                    break;
                }
                if(!lastConnectionStatus){
                    lastConnectionStatus=true;
                    logger.info("MQ状态为正常");
                }
            }
            catch (Exception e) {
                logger.error("获取MQ消息出错!"+e.toString());
                if(lastConnectionStatus){
                    lastConnectionStatus=false;
                }
            }
        }
    }


    private int getMessageType(String msg){

        if(msg.contains((char)2+"MAS")){

            return TYPE_MAS;
        }
        else if(msg.contains("REQFPN")||msg.contains("RESFPN")){

            return TYPE_FPN;
        }
        else if(msg.contains("REQPWI")||msg.contains("RESPWI")){

            return TYPE_PWI;
        }

        else
            return TYPE_UNDEFINED;
    }

    private void handleInvalidMsg(String msg){

        logger.info("Received unparsed message:\r\n"+msg+"\r\n");


    }
}